package logic

import (
	"community/models/redisinit"
	"context"
	"fmt"
	"math/rand"
	"time"

	"community/communityrpc/communityrpc"
	"community/communityrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
	return &SendSmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SendSmsLogic) SendSms(in *communityrpc.SendSmsRequest) (*communityrpc.SendSmsResponse, error) {
	// todo: add your logic here and delete this line
	get := redisinit.Client.Get("Send" + in.Account + in.Source)
	if get.Val() != "" {
		return nil, fmt.Errorf("短信每分钟只能发送一次")
	}
	code := rand.Intn(9000) + 1000
	//sms, err := pkg.SendSms(in.Account, strconv.Itoa(code))
	//if err != nil {
	//	return nil, err
	//}
	//
	//if *sms.Body.Code != "OK" {
	//	return nil, fmt.Errorf(*sms.Body.Message)
	//}
	redisinit.Client.Set("SendSms"+in.Account+in.Source, code, time.Minute*5)
	redisinit.Client.Set("Send"+in.Account+in.Source, 1, time.Minute*1)
	return &communityrpc.SendSmsResponse{}, nil
}
